package com.tjb.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;
import com.tjb.reggie.common.R;
import com.tjb.reggie.common.TjbException;
import com.tjb.reggie.entity.Category;
import com.tjb.reggie.entity.Dish;
import com.tjb.reggie.entity.Setmeal;
import com.tjb.reggie.service.CategoryService;
import com.tjb.reggie.service.DishService;
import com.tjb.reggie.service.SetMealService;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author tjb
 * @version 1.0
 * @date 2022/8/25 11:34
 * 分类管理
 */
@RestController
@RequestMapping("/category")
public class CategoryController {
    /**
     * 分类业务
     */
    @Resource
    private CategoryService categoryService;
    /**
     * 菜品业务
     */
    @Resource
    private DishService dishService;
    /**
     * 套餐业务
     */
    @Resource
    private SetMealService setMealService;
    /**
     * 新增菜品分类
     */
    @PostMapping
    public R<String> save(@RequestBody Category category){
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Category::getName, category.getName());
        Category one = categoryService.getOne(wrapper);
        if (one !=null){
            return R.error("已经存在该分类名称了，请换一个名称在试一试");
        }
        categoryService.save(category);
        return R.success("新增分类成功");
    }
    /**
     * 分页
     */
    @GetMapping("page")
    public R<Page<Category>> page(int page, int pageSize){
        //分页构造器
        Page<Category> categoryPage = new Page<>(page, pageSize);
        //条件构造器
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        //排序实现
        wrapper.orderByAsc(Category::getSort);
        //根据分页条件查询
        categoryService.page(categoryPage, wrapper);
        //返回
        return R.success(categoryPage);
    }
    @DeleteMapping
    public R<String> deleteById(Long ids){
        System.out.println(ids);
        //菜品
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId, ids);
        int countOfDis = dishService.count(dishLambdaQueryWrapper);
        if (countOfDis > 0){
           throw new TjbException("当前分类关联了菜品，不能删除");
        }
        //套餐
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId, ids);
        int countOfSetMeal = dishService.count(dishLambdaQueryWrapper);
        if (countOfSetMeal > 0){
            throw new TjbException("当前分类关联了套餐，不能删除");
        }

        boolean b = categoryService.removeById(ids);
        return b ? R.success("删除成功") : R.error("删除失败");
    }

    /**
     * 根据id修改分类
     * @param category
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Category category){
        categoryService.updateById(category);
        return R.success("更新成功");
    }

    /**
     * 根据类型查询分类集合
     * @param category
     * @return
     */
    @GetMapping("list")
    public R<List<Category>> list(Category category){
        //条件构造器
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        //条件构造
        wrapper.eq(category.getType()!=null, Category::getType, category.getType());
        wrapper.orderByAsc(Category::getSort).orderByAsc(Category::getCreateTime);
        //查询
        List<Category> list = categoryService.list(wrapper);
        return R.success(list);
    }
}
